package com.projectc.db.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.projectc.db.EMF;
import com.projectc.db.entities.User;

public class UsersDAO extends GenericDaoImpl<User> {

	@SuppressWarnings("unchecked")
	public List<User> getAllUsers() {
		EntityManager em = EMF.get().createEntityManager();
		try {
			Query query = em.createQuery("Select u From User u");
			List<User> users = query.getResultList();
			return users;
		} finally {
			em.close();
		}
	}

	public User loadUserByUsername(String username) {
		EntityManager em = EMF.get().createEntityManager();
		try {
			Query query = em.createQuery("select u FROM User u where u.username= :username");
			query.setParameter("username", username);
			@SuppressWarnings("unchecked")
			List<User> users = query.getResultList();
			if (users != null && users.size() == 1) {
				return users.get(0);
			}
			return null;
		} finally {
			em.close();
		}
	}

}
